package Atm01;

import Tz1.Strin;

import java.awt.print.Book;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Properties;

public class FileUtil {

    /**
     * 对象反序列化
     * @param path - 读取的文件路径
     * @return
     */
    public static Object readObject(String path) {

        ObjectInputStream objread= null;
        try {
            FileInputStream in=new FileInputStream(path);
            objread = new ObjectInputStream(in);
            try {
                Object map=objread.readObject();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (objread!=null){

                try {
                    objread.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return null;


    }


    /**
     * 对象序列化
     * @param path - 保存的文件路径
     * @param obj - 序列化的对象
     */
    public static void WriteObject(String path,Object obj){
        File f=new File(path);
        FileOutputStream out= null;
        try {
            out = new FileOutputStream(f);
            ObjectOutputStream objwrite=new ObjectOutputStream(out);
            objwrite.writeObject(obj);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }

    }

    public static void main(String[] args) {
        FileUtil fileUtil=new FileUtil();
            ArrayList users =new ArrayList();
            users.add(new User("aaaaaa","111111",1000));
            users.add(new User("bbbbbb","222222",2000));
            users.add(new User("cccccc","333333",3000));
            users.add(new User("dddddd","444444",4000));
            users.add(new User("eeeeee","555555",5000));
            WriteObject("User.txt","users");
            readObject("User.txt");
        }

    }

